package com.neusoft.neumooc.dao;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

import org.springframework.stereotype.Repository;

import com.neusoft.core.common.BaseDAO;
import com.neusoft.core.common.exception.DAOException;
import com.neusoft.neumooc.vo.ColumnVO;

@Repository
public class ColumnDAOImpl extends BaseDAO implements ColumnDAO {

    private static final String SQL_GET_ALL_COLUMNS = 
        "SELECT columnId as columnId, " +
               "columnName as columnName, " +
               "typeId as typeId, " +
               "listTplFile as listTplFile, " +
               "pageTplFile as pageTplFile, " +
               "canSearch as canSearch " +
        "FROM cw_columns";
        
    private static final String SQL_GET_COLUMN_BY_ID = 
        "SELECT columnId as columnId, " +
               "columnName as columnName, " +
               "typeId as typeId, " +
               "listTplFile as listTplFile, " +
               "pageTplFile as pageTplFile, " +
               "canSearch as canSearch " +
        "FROM cw_columns WHERE columnId = :columnId";
        
    private static final String SQL_ADD_COLUMN = 
        "INSERT INTO cw_columns(columnId, columnName, typeId, listTplFile, pageTplFile, canSearch) " +
        "VALUES (:columnId, :columnName, :typeId, :listTplFile, :pageTplFile, :canSearch)";
        
    private static final String SQL_UPDATE_COLUMN = 
        "UPDATE cw_columns SET columnName = :columnName, typeId = :typeId, " +
        "listTplFile = :listTplFile, pageTplFile = :pageTplFile, " +
        "canSearch = :canSearch " +
        "WHERE columnId = :columnId";
        
    private static final String SQL_DELETE_COLUMN = 
        "DELETE FROM cw_columns WHERE columnId = :columnId";

    @Override
    public List<ColumnVO> getAllColumns() throws DAOException {
        try {
            List<ColumnVO> columns = this.queryForBeanList(SQL_GET_ALL_COLUMNS, ColumnVO.class);
            log.debug("查询到的栏目列表: " + columns);
            return columns;
        } catch (Exception e) {
            log.error("查询栏目列表失败", e);
            throw new DAOException("查询栏目列表失败", e);
        }
    }

    @Override
    public ColumnVO getColumnById(String columnId) throws DAOException {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("columnId", columnId);
            log.info("=======" + columnId);
            ColumnVO column = this.queryForBeanByBeanMap(SQL_GET_COLUMN_BY_ID, ColumnVO.class, params);
            log.info("查询到的栏目: " + column);
            return column;
        } catch (Exception e) {
            log.error("查询栏目失败", e);
            throw new DAOException("查询栏目失败", e);
        }
    }

    @Override
    public boolean addColumn(ColumnVO column) throws DAOException {
        try {
            this.update(SQL_ADD_COLUMN, column);
            return true;
        } catch (Exception e) {
            log.error("添加栏目失败", e);
            return false;
        }
    }

    @Override
    public boolean updateColumn(ColumnVO column) throws DAOException {
        try {
            this.update(SQL_UPDATE_COLUMN, column);
            return true;
        } catch (Exception e) {
            log.error("更新栏目失败", e);
            return false;
        }
    }

    @Override
    public boolean deleteColumn(String columnId) throws DAOException {
        try {
            ColumnVO column = new ColumnVO();
            column.setColumnId(columnId);
            this.update(SQL_DELETE_COLUMN, column);
            return true;
        } catch (Exception e) {
            log.error("删除栏目失败", e);
            return false;
        }
    }
} 